package com.ivy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ivy.entity.School;
import com.ivy.entity.Teacher;
import com.ivy.service.TeacherService;
import com.ivy.service.impl.TeacherServiceImpl;
import com.ivy.utils.DataReadUtils;

public class TeacherServlet extends HttpServlet {

	public TeacherServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}
	private TeacherService tservice = new TeacherServiceImpl();
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//School school = DataReadUtils.copyBean(request, School.class);

		List<Teacher> teachers = tservice.getAllTeacher();
		
		JSONArray arr = new JSONArray();
		for(int i=0;i<teachers.size();i++) {
			Teacher t = teachers.get(i);
			JSONObject obj = new JSONObject();
			
			try {
				//obj.put("id", t.getId());
				obj.put("tname", t.getTname());
				obj.put("tid", t.getTid());
				obj.put("sid", t.getSid());
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
			arr.put(obj);
			
		}
		
		response.getWriter().print(arr);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
